package com.atguigu.aop.config.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 使用AspectJ间接配置，一个类就足够了
 */
@Aspect
@Component
public class AopConfig {

//    /**
//     * 切入点
//     */
//    @Pointcut("execution(* com.atguigu.aop.service.*.*(..))")
//    public void pointCut(){}
//
//    public static void main(String[] args) {
//        List<String> aa = Collections.singletonList("aa");
//        System.out.println(aa.size());
//    }
//
//    /**
//     * 通知
//     * @param joinPoint
//     * @return
//     */
//    @Around("pointCut()")
//    public Object around(ProceedingJoinPoint joinPoint) {
//        System.out.println("go go go MyAspect process!!!");
//        Object obj = null;
//        try {
//            obj = joinPoint.proceed();
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//        }
//        return obj;
//    }

//    @Pointcut("execution(* com.atguigu.aop.service.*.*(..))")
//    public void webLog() {
//    }

    @Pointcut(value = "@annotation(com.atguigu.aop.config.advisor.MyAnnotation)")
    public void webLog() {
    }

//    @Before("webLog()")
//    public void doBefore(JoinPoint joinPoint) throws Throwable {
//        //接收到请求，记录请求内容
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//
//        //记录下请求内容
//        System.out.println("URL: " + request.getRequestURL().toString());
//        System.out.println("HTTP_METHOD: " + request.getMethod());
//        System.out.println("IP: " + request.getRemoteAddr());
//        System.out.println("CLASS_METHOD: " + joinPoint.getSignature().getDeclaringTypeName() + "." +
//                joinPoint.getSignature().getName());
//        System.out.println("ARGS: " + Arrays.toString(joinPoint.getArgs()));
//    }
//
//    @AfterReturning(returning = "ret", pointcut = "webLog()")
//    public void doAfterReturning(Object ret) throws Throwable {
//        //处理完请求，返回内容
//        System.out.println("方法的返回值：" + ret);
//    }
//
//    @AfterThrowing("webLog()")
//    public void throwss(JoinPoint jp) {
//        //后置异常通知
//        System.out.println("方法异常时执行...");
//    }
//
//    @After("webLog()")
//    public void after(JoinPoint jp) {
//        //后置最终执行，不管是正常退出还是抛出异常都执行
//        System.out.println("方法最后执行...");
//    }

    @Around("webLog()")
    public Object arround(ProceedingJoinPoint jp) {
        System.out.println("方法环绕执行...");
        try {
            Object o = jp.proceed();
        System.out.println("方法环绕执行2...");
            System.out.println("方法环绕proceed，结果是：" + o);
            return o;
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
    }


}
